package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminsetmealcontroller")
@Slf4j
@RequestMapping("/admin/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 分页查询套餐  条件查询
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> getPageList(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("接收到前端传过来的分页数据为:{}",setmealPageQueryDTO);
       PageResult pageResult =  setmealService.getPageList(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 新增套餐
     * @param dto
     * @return
     */
    @PostMapping
    @CacheEvict(cacheNames = "setmeal",key = "#dto.categoryId") //清理redis缓存
    public Result save(@RequestBody SetmealDTO dto){
        log.info("接收到前端传过来的新增套餐数据为:{}",dto);
        setmealService.save(dto);
        return Result.success();
    }

    /**
     * 根据id批量删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(cacheNames = "setmeal",allEntries = true) //清理redis缓存
    public Result deleteByIds(@RequestParam List<Long> ids){
        log.info("接收到前端传过来的要批量删除的id为:{}",ids);
        setmealService.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 通过套餐id查找套餐及所属菜品
     * @return
     */
    @GetMapping("/{id}")
    public Result<SetmealVO> getById(@PathVariable Long id){
        log.info("从前端传过来的根据id查找套餐的id为:{}",id);
       SetmealVO setmealVO= setmealService.selectById(id);
        return Result.success(setmealVO);
    }

    /**
     * 修改套餐属性
     * @param setmealDTO
     * @return
     */
    @PutMapping
    @CacheEvict(cacheNames = "setmeal",allEntries = true) //清理redis缓存
    public Result update(@RequestBody SetmealDTO setmealDTO){
        log.info("接收到前端传过来的修改套餐属性数据为:{}",setmealDTO);
        setmealService.update(setmealDTO);
        return Result.success();
    }

    /**
     * 修改套餐状态
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmeal",allEntries = true) //清理redis缓存
    public Result setStatus(@PathVariable Integer status,Long id){
        log.info("接收到的前端传过来的修改状态是数据为:{}{}",status==1?"起售":"下架",id);
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setmealService.setStatus(setmeal);
        return Result.success();
    }
}
